package com.design.twords.fragment;

import androidx.appcompat.app.AlertDialog;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.design.twords.activity.BooklListActivity;
import com.design.twords.activity.OcrActivity;
import com.design.twords.activity.SearchActivity;
import com.design.twords.R;
import com.design.twords.adatper.FloderListAdapter;
import com.design.twords.database.BookList;
import com.design.twords.utils.MyApplication;

import org.litepal.LitePal;

import java.util.ArrayList;

public class WordFragment extends Fragment {

    private RecyclerView floderList;
    private TextView searchTextView;
    private LinearLayout empty_floderlist, searchLinearLayout;
    private ImageView word_fresh;
    private Button download_book;
    private TextView start_ocr;

    private FloderListAdapter floderListAdapter;

    private ArrayList<BookList> bookLists = new ArrayList<>();

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.word_fragment, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //添加到Activity 列表中
        MyApplication.getInstance().addActivity(this.getActivity());

        initView();

        getBookList();

        floderList.setLayoutManager(new LinearLayoutManager(this.getActivity(), RecyclerView.VERTICAL,false));
        floderListAdapter = new FloderListAdapter(getContext(), bookLists, R.layout.item_floder);
        floderList.setAdapter(floderListAdapter);

        floderListAdapter.setListener(new FloderListAdapter.ItemClickListener() {
            @Override
            public void itemLongClickListener(int position) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setItems(new String[]{"删除"}, (DialogInterface.OnClickListener) (dialog, which) -> {
                    floderListAdapter.deleteItem(position);
                    Toast.makeText(getActivity(), "删除成功", Toast.LENGTH_LONG).show();

                    onStart();

                }).show();
            }
        });


        //点击搜索栏 跳转到搜索界面
        searchTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), SearchActivity.class);
                startActivity(intent);
            }
        });

        word_fresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onStart();
            }
        });

        download_book.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), BooklListActivity.class);
                startActivity(intent);

            }
        });

        start_ocr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), OcrActivity.class);
                startActivity(intent);
            }
        });

    }


    public void getBookList(){
        bookLists.clear();
        bookLists = (ArrayList<BookList>) LitePal.findAll(BookList.class);
        if(bookLists.size() > 0){
            empty_floderlist.setVisibility(View.GONE);
            Log.i("booklists.size", bookLists.size()+"");

        }else{
            empty_floderlist.setVisibility(View.VISIBLE);

        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }


    @Override
    public void onStart() {
        super.onStart();

        getBookList();
        floderListAdapter.setBookLists(bookLists);
        floderList.setAdapter(floderListAdapter);

    }


    private void initView(){
        floderList = getActivity().findViewById(R.id.floder_list);
        searchTextView = getActivity().findViewById(R.id.wordfragment_tv);
        empty_floderlist = getActivity().findViewById(R.id.empty_floderlist);
        word_fresh = getActivity().findViewById(R.id.word_fresh);

        download_book = getActivity().findViewById(R.id.download_book);
        start_ocr = getActivity().findViewById(R.id.start_ocr);
        searchLinearLayout = getActivity().findViewById(R.id.wordfragment_search);
    }



}